﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;

namespace NthDownload.Common.Interactivity.Triggers
{
    public enum ScrollTriggerDeltaDirection
    {
        Horizontal,
        Vertical,
    }
    
    public class SrollTrigger : System.Windows.Interactivity.EventTrigger
    {
        #region Override Methods

        protected override void OnEvent(EventArgs eventArgs)
        {
            base.OnEvent(eventArgs);

            if (TargetScrollViewer == null)
            {
                return;
            }

            double actualDelta = Delta;
            MouseWheelEventArgs mouseWheelEventArgs = eventArgs as MouseWheelEventArgs;
            if (mouseWheelEventArgs != null)
            {
                actualDelta = mouseWheelEventArgs.Delta;
            }

            if (actualDelta != 0)
            {
                switch (DeltaDirection)
                {
                    case ScrollTriggerDeltaDirection.Horizontal:
                        TargetScrollViewer.ScrollToHorizontalOffset(TargetScrollViewer.HorizontalOffset + actualDelta);
                        break;
                    case ScrollTriggerDeltaDirection.Vertical:
                        TargetScrollViewer.ScrollToVerticalOffset(TargetScrollViewer.VerticalOffset + actualDelta);
                        break;
                }
            }
        }

        #endregion Override Methods

        #region Dependancy Properties

        #region Target Scroll Viewer

        public ScrollViewer TargetScrollViewer
        {
            get { return (ScrollViewer)GetValue(TargetScrollViewerProperty); }
            set { SetValue(TargetScrollViewerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TargetScrollViewer.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetScrollViewerProperty =
            DependencyProperty.Register("TargetScrollViewer", typeof(ScrollViewer), typeof(SrollTrigger), new PropertyMetadata(null));

        #endregion Target Scroll Viewer

        #region Delta

        public double Delta
        {
            get { return (double)GetValue(DeltaProperty); }
            set { SetValue(DeltaProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Delta.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DeltaProperty =
            DependencyProperty.Register("Delta", typeof(double), typeof(SrollTrigger), new PropertyMetadata(Convert.ToDouble(0)));

        #endregion Delta

        #region Delta Direction

        public ScrollTriggerDeltaDirection DeltaDirection
        {
            get { return (ScrollTriggerDeltaDirection)GetValue(DeltaDirectionProperty); }
            set { SetValue(DeltaDirectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DeltaDirection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DeltaDirectionProperty =
            DependencyProperty.Register("DeltaDirection", typeof(ScrollTriggerDeltaDirection), typeof(SrollTrigger), new PropertyMetadata(ScrollTriggerDeltaDirection.Vertical));

        #endregion Delta Direction

        #endregion Dependancy Properties
    }
}
